﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace healthdemo
{
    public partial class FormJiBingTongJi : Form
    {
        public FormJiBingTongJi()
        {
            InitializeComponent();
            InitData();
            dataGridView1.DataSource = userinformation.GetAllList().Tables[0];
            this.dataGridView1.AllowUserToAddRows = false;
        }

        HealthExaminationSys.BLL.userinformation userinformation = new HealthExaminationSys.BLL.userinformation();
        //初始化数据
        private void InitData()
        {
            //姓名
            col_xingming.DataPropertyName = "name";

            //性别
            col_xingbie.DataPropertyName = "sex";

            //年龄
            col_nianling.DataPropertyName = "age";

            //身份证号
            col_shenfenzh.DataPropertyName = "idcard";

            //体检日期
            col_tijianrq.DataPropertyName = "date";

            //电话号码
            col_dianhua.DataPropertyName = "tel";

            //体检套餐
            col_taocan.DataPropertyName = "pakage";

            //体检状态
            col_zhuangtai.DataPropertyName = "status";

            //村居
            col_cunju.DataPropertyName = "adress";

            dataGridView1.AutoGenerateColumns = false;

            string[] lstStr = { "全部","A型", "B型", "C型" };
            cmbJiBing.Items.AddRange(lstStr);
            cmbJiBing.SelectedItem = "A型";
            DateTime curDate = DateTime.Now;
            //开始日期
            dtStart.Value = new DateTime(curDate.Year,1,1);
            //结束日期
            dtEnd.Value = dtStart.Value.AddYears(1).AddDays(-1);

            //体检状态
            DataTable dtLeiBie = new HealthExaminationSys.BLL.sys_item().GetList("itemType ='RYZT'").Tables[0];
            AddNewRow(dtLeiBie);

            cmbTijianZhuangTai.DataSource = dtLeiBie;
            cmbTijianZhuangTai.DisplayMember = "itemText";
            cmbTijianZhuangTai.ValueMember = "itemCode";

 
        }

        private void AddNewRow(DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dr["itemType"] = "RYZT";
            dr["itemCode"] = "-1";
            dr["itemText"] = "全部";
            dr["xianshixh"] = -1;
            dt.Rows.InsertAt(dr, 0);
        }

        //查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strwhere = "date between'" + dtStart.Value + "'and '"+ dtEnd.Value + "'";

            if (cmbJiBing.Text != "全部")
            {
                strwhere = strwhere + "and pakage = '" + cmbJiBing.Text + "'";
            }

            if (textNameBox.Text != "") {
                strwhere = strwhere + " and name = '" + textNameBox.Text + "'";
            }
            if (cmbTijianZhuangTai.Text != "全部") {
                strwhere = strwhere + " and status = '" + cmbTijianZhuangTai.Text + "'";
            }

            dataGridView1.DataSource = userinformation.GetList(strwhere).Tables[0];
            this.dataGridView1.AllowUserToAddRows = false;
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //userinformation.Delete
            var rows = dataGridView1.SelectedRows;
            if (rows.Count > 0)
            {
                var row = rows[0];
                if (userinformation.Delete(((DataRowView)row.DataBoundItem).Row["id"].ToString()))
                {

                    MessageBox.Show("删除成功！");
                    dataGridView1.DataSource = userinformation.GetAllList().Tables[0];
                }
                else
                {
                    MessageBox.Show("删除失败!");
                }
            }
        }

        //更新
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var rows = dataGridView1.SelectedRows;
            if (rows.Count > 0)
            {
                var row = rows[0];
                new Update(((DataRowView)row.DataBoundItem).Row).ShowDialog();
                dataGridView1.DataSource = userinformation.GetAllList().Tables[0];
            }
        }
        //添加
        private void btnAdd_Click(object sender, EventArgs e)

        {
            new Add().ShowDialog();
            dataGridView1.DataSource = userinformation.GetAllList().Tables[0];

        }
    }
}
